package com.example.infowall02.service.impl;

import com.example.infowall02.domain.User;
import com.example.infowall02.mapper.UserMapper;
import com.example.infowall02.service.UserService;
import com.example.infowall02.utils.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //通过id查找用户
    @Override
    public User findUserById(int id) {
        return userMapper.getUserById(id);
    }
    //注册
    @Override
    public int register(User user) {
        // 对密码进行 MD5 加密
        user.setPassword(MD5Util.md5(user.getPassword()));
        return userMapper.saveUser(user);
    }
    //登录
    @Override
    public User login(String username, String password) {
        return userMapper.getUserByUsernameAndPassword(username,MD5Util.md5(password));
    }
    //修改昵称
    @Override
    public User updateNickname(String nickname, int id) {
        int num = userMapper.updateUserNickname(nickname, id);
        if(num>0){
            return userMapper.getUserById(id);
        }
        return null;
    }
    //修改头像
    @Override
    public User updateUserImage(String userImage, int id) {
        int num = userMapper.updateUserImage(userImage, id);
        if(num>0){
           return userMapper.getUserById(id);
        }
        return null;
    }
    //修改密码
    @Override
    public User updateUserPassword(String password, int id) {
        int num = userMapper.updatePassword(MD5Util.md5(password), id);
        if(num>0){
           return userMapper.getUserById(id);
        }
        return null;
    }
}
